package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstIntegralRuleDTO;
import com.financing.wap.service.IDstIntegralRuleService;
import com.financing.wap.util.DTOdateSetUtil;
import com.financing.wap.web.controller.BaseController;

@Controller("dstIntegralRuleController")
@RequestMapping("/api/dstIntegralRule")
public class DstIntegralRuleController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstIntegralRuleService dstIntegralRuleService;
	
	@RequestMapping("/listDstIntegralRule")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, DstIntegralRuleDTO dstIntegralRule,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query dstIntegralRule list begin .......");
			
			dstIntegralRule.setBegin(pager.getStartIndex());
			dstIntegralRule.setPageSize(pager.getPageSize());
			
			int count = dstIntegralRuleService.queryListCount(dstIntegralRule);
			
			pager.setTotalCount(count);
			
			List<DstIntegralRuleDTO> dstIntegralRuleList = null;
			
			if(count > 0){
				dstIntegralRuleList = dstIntegralRuleService.queryList(dstIntegralRule);
			}else{
				dstIntegralRuleList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", dstIntegralRuleList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query dstIntegralRule list end .......");
		}catch(Exception e){
			log.error("query dstIntegralRule error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addDstIntegralRule")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, DstIntegralRuleDTO dstIntegralRule){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add dstIntegralRule  begin .......");
			
			DTOdateSetUtil.setCreateInfo(dstIntegralRule);
			DTOdateSetUtil.setUpdateInfo(dstIntegralRule);
			
			int id = dstIntegralRuleService.insert(dstIntegralRule);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add dstIntegralRule list end .......");
		}catch(Exception e){
			log.error("add dstIntegralRule error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailDstIntegralRule")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, DstIntegralRuleDTO dstIntegralRule){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail dstIntegralRule  begin .......");
			
			Integer id = dstIntegralRule.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DstIntegralRuleDTO dto = dstIntegralRuleService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail dstIntegralRule  end .......");
		}catch(Exception e){
			log.error("query detail dstIntegralRule error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteDstIntegralRule")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, DstIntegralRuleDTO dstIntegralRule){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete dstIntegralRule  begin .......");
			
			Integer id = dstIntegralRule.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			dstIntegralRuleService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete dstIntegralRule  end .......");
		}catch(Exception e){
			log.error("delete dstIntegralRule error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateDstIntegralRule")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, DstIntegralRuleDTO dstIntegralRule){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update dstIntegralRule  begin .......");
			
			Integer id = dstIntegralRule.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(dstIntegralRule);
			
			dstIntegralRuleService.update(dstIntegralRule);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update dstIntegralRule list end .......");
		}catch(Exception e){
			log.error("update dstIntegralRule error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceDstIntegralRule")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, DstIntegralRuleDTO dstIntegralRule){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce dstIntegralRule  begin .......");
			
			Integer id = dstIntegralRule.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSetUtil.setUpdateInfo(dstIntegralRule);
			
			dstIntegralRuleService.updateForce(dstIntegralRule);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce dstIntegralRule list end .......");
		}catch(Exception e){
			log.error("updateForce dstIntegralRule error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
